﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


//TODO: HAVE TO REBUILD WITHOUT USING SPLIT PANEL CONTAINER AS IT'S MESSING UP THE CODE
//EVENTUALLY CLICKING NODE (A FRIEND) WILL OPEN A NEW CHAT AND CLICKING NEW ROOM
//MENU ITEM WILL DO THE SAME AFTER SOME SETTING DIALOG (SUCH AS NAME OF ROOM)
namespace Chat_D_LuX
{
    
    public partial class frmMain : Form
    {
        //private ChatServiceRef.User user;
        
        public frmMain()
        {
            InitializeComponent();
            //logIn.ShowDialog();
        }

        //With this being eventually user control based instead of forms this may disappear, not sure however
        private void frmMain_MdiChildActivate(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild == null)
            {
                tabctlMain.Visible = false;
            }
                // If no any child form, hide tabControl, should always start with lobby however
            else
            {
                this.ActiveMdiChild.WindowState = FormWindowState.Maximized;// Child form always maximized 

                // If child form is new and no has tabPage, create new tabPage 
                if (this.ActiveMdiChild.Tag == null)
                {
                    // Add a tabPage to tabControl with child 
                    // form caption 
                    TabPage tp = new TabPage(this.ActiveMdiChild.Text);
                    tp.Tag = this.ActiveMdiChild;
                    tp.Parent = tabctlMain;
                    tabctlMain.SelectedTab = tp;

                    this.ActiveMdiChild.Tag = tp;
                    this.ActiveMdiChild.FormClosed +=
                        new FormClosedEventHandler(ActiveMdiChild_FormClosed);
                }
                if (!tabctlMain.Visible) tabctlMain.Visible = true;
            }
        }//end frmMain_MdiChildActivate

        private void ActiveMdiChild_FormClosed(object sender, FormClosedEventArgs e)
        {
            ((sender as Form).Tag as TabPage).Dispose();
        }//end ActiveMdiChild_FormClosed

        private void createRoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Should take a string to represent room name, not sure how to handle yet
            newChatRoom();           
        }

        private void tabctlMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((tabctlMain.SelectedTab != null) && (tabctlMain.SelectedTab.Tag != null))
            {
                (tabctlMain.SelectedTab.Tag as Form).Select();
            }
        }//end selected index changed

        private void chooseThemeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //TODO this should have theme stuff not a frmRoom that will be deleted soon
            frmRoom frm = new frmRoom();
            frm.MdiParent = this;
            frm.Show();
        }//end choose theme

        //TODO This will eventually take atleast a string variable representing name of chatroom
        private void newChatRoom() 
        {
            //TODO This menu should create a custom user control "ChatRoom" and push this into the tab control
            Controls.ChatRoom cr = new Controls.ChatRoom();//new room
            cr.Dock = DockStyle.Fill;//fill tab page
            TabPage tp = new TabPage("Use Text Variable Here");//This should have a room name variable
            tabctlMain.TabPages.Add(tp);//add tab page to control
            tp.Controls.Add(cr);//add chatroom to tab page
        }

        private void tvMain_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //TODO Eventually should pass user name to chatroom to signify "private chat"
            newChatRoom();
        }

        private void signOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //TODO Signs out user, resets frmMain
        }

        private void editProfileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //TODO the controls on the ucAccountEdit control need filled at this point with particular user data, unsure now how to do that
            Chat_D_LuX.Controls.ucAccountEdit ae = new Chat_D_LuX.Controls.ucAccountEdit();
            ae.Dock = DockStyle.Fill;
            TabPage tp = new TabPage("Username Variable Here");//This should have username for the user that selected this menu item
            tabctlMain.TabPages.Add(tp);
            tp.Controls.Add(ae);
            //TODO build profile user control
        }
    }
}
